package nu.mine.tbje.easyEDT;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shale.view.AbstractViewController;

public class Navigation extends AbstractViewController{
	
	private String id;

	public String saveId(){
		try {
			cookieSet(Integer.parseInt(id));
			return "ok";
		} catch (NumberFormatException e) {
			return "error";
		}
	}
	
	public String getRequestId(){
        FacesContext context = FacesContext.getCurrentInstance();
        id = ((HttpServletRequest) context.getExternalContext().getRequest()).getParameter("id");
        if (id==null){
        	return "";
        }
		return "&id="+id; 
	}
	
	public int getId() {
		int res = 0;
        FacesContext fc = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
        String groupeId = request.getParameter("id");
        Cookie cookie = (Cookie)fc.getExternalContext().getRequestCookieMap().get("groupeId");
		try {
			res = Integer.parseInt(groupeId);
		} catch (NumberFormatException e) {
	        if (cookie != null) {
	        	try {
					res = Integer.parseInt(cookie.getValue());
				} catch (NumberFormatException e1) {
					res = 870;
				}
	        }
		}
        return res;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNext(){
		String date;
		Date d_date;
		String res;
		SimpleDateFormat d = new SimpleDateFormat("ddMMyyyy");
        FacesContext context = FacesContext.getCurrentInstance();
        date = ((HttpServletRequest) context.getExternalContext().getRequest()).getParameter("date");
		if (date != null) {
			try {
				d_date = d.parse(date);
				res = next(d_date);
			} catch (Exception e) {
				res = next(new Date());
			}
		} else {
			res = next(new Date());
		}
		return res;
	}

	public String getPrev(){
		String date;
		Date d_date;
		String res;
		SimpleDateFormat d = new SimpleDateFormat("ddMMyyyy");
        FacesContext context = FacesContext.getCurrentInstance();
        date = ((HttpServletRequest) context.getExternalContext().getRequest()).getParameter("date");
		if (date != null) {
			try {
				d_date = d.parse(date);
				res = prev(d_date);
			} catch (Exception e) {
				res = prev(new Date());
			}
		} else {
			res = prev(new Date());
		}
		return res;
	}
	
	public int getWeek(){
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(new Date());
		return cal.get(Calendar.WEEK_OF_YEAR);
	}
	
	public String next(Date date){
		Calendar cal = GregorianCalendar.getInstance();
		SimpleDateFormat d = new SimpleDateFormat("ddMMyyyy");
		cal.setTime(date);
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.SATURDAY:
			cal.add(Calendar.DAY_OF_MONTH, +2);
			break;
		case Calendar.FRIDAY:
			cal.add(Calendar.DAY_OF_MONTH, +3);
			break;
		default:
			cal.add(Calendar.DAY_OF_MONTH, +1);
			break;
		}
		date = cal.getTime();
		return d.format(date);
	}
	

	public String prev(Date date){
		Calendar cal = GregorianCalendar.getInstance();
		SimpleDateFormat d = new SimpleDateFormat("ddMMyyyy");
		cal.setTime(date);
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.MONDAY:
			cal.add(Calendar.DAY_OF_MONTH, -3);
			break;
		case Calendar.SUNDAY:
			cal.add(Calendar.DAY_OF_MONTH, -2);
			break;
		default:
			cal.add(Calendar.DAY_OF_MONTH, -1);
			break;
		}
		date = cal.getTime();
		return d.format(date);
	}

	public String getDate(){
		SimpleDateFormat d = new SimpleDateFormat("ddMMyyyy");
		String sDate;
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
		sDate = request.getParameter("date");
		if (sDate==null){
			sDate = d.format(new Date());
		}
		return sDate;
	}
	
	
	private static void cookieSet (int groupeId) {
        FacesContext context = FacesContext.getCurrentInstance();
        Cookie cookie = new Cookie("groupeId", ""+groupeId);
        cookie.setMaxAge(Integer.MAX_VALUE);
        cookie.setPath("/");
        ((HttpServletResponse) context.getExternalContext().getResponse()).addCookie(cookie);
		
	}
	
}
